package com.sprint.controller;

import com.sprint.entities.CommonResult;
import com.sprint.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

@Slf4j //打印日志
@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

    @Resource
    private DiscoveryClient discoveryClient;

    @RequestMapping("/zk")
    public String paymentzk(){
        return "Spring cloud with zookeeper:" + serverPort+"\t"+ UUID.randomUUID().toString();
    }

    @GetMapping("/discovery")
    public  Object discovery(){

        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("*****" + service);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info(instance.getUri() + "---- ------" +   instance.getServiceId());
        }
        return this.discoveryClient;
    }
}
